home *** CD-ROM | disk | FTP | other *** search
- Path: longwood.cs.ucf.edu!not-for-mail
- From: schnitzi@longwood.cs.ucf.edu (Mark Schnitzius)
- Newsgroups: comp.lang.c
- Subject: Sorting large files
- Date: 25 Jan 1996 13:48:11 -0500
- Organization: University of Central Florida
- Message-ID: <4e8j9b$cuf@longwood.cs.ucf.edu>
- NNTP-Posting-Host: longwood.cs.ucf.edu
-
- There was some discussion here a little while
- back on how to sort the lines in a large file
- without having to have a huge character array.
- I suggested using ftell and fseek to hunt down
- the particular lines you are comparing. Only
- just the other day did I notice (via a web
- search engine) that someone posted a question
- on how this could be done... So here goes.
-
-
- This program sorts stdin to stdout, so it would
- have to be run like this:
-
- sort < infile > outfile
-
- It should be easy enough to modify to use file
- pointers, though. It also requires a 'long'
- for every line in the file, but this beats the
- heck out of a huge character array. I've
- written it here to handle up to 5000-line files.
-
-
- #include <stdio.h>
-
- main()
- {
- long index[5000], z;
-
- char line1[ 256 ], line2[ 256 ];
-
- int i, j, count=0;
-
- do
- {
- index[ count++ ] = ftell( stdin );
- }
- while ( gets(line1) );
-
- for ( i=0; i<count-1; i++ )
- {
- for ( j=i+1; j<count; j++ )
- {
- fseek( stdin, index[i], 0 );
- gets( line1 );
- fseek( stdin, index[j], 0 );
- gets( line2 );
-
- if ( strcmp( line1, line2 ) > 0 )
- {
- z = index[i];
- index[i] = index[j];
- index[j] = z;
- }
- }
- }
-
- for ( i=0; i<count; i++ )
- {
- fseek( stdin, index[i], 0 );
- gets( line1 );
- puts( line1 );
- }
- }
-
-
- _____________________________________________________________
- mark schnitzius - - - - - - - - - - - - - schnitzi@mentos.com
- - - - -<a href="http://east.isx.com/~schnitzi/">me</a>- - - -
-